﻿using NetBase.Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xaml;

namespace NetBase.View
{
    /// <summary>
    /// Interaction logic for SqImgLayerView.xaml
    /// </summary>
    public partial class SqImgLayerView : UserControl
    {
        private SqImgLayer layer;

        private static double GridViewScaleValue = 3;

        private bool IsGridView { get; set; }

        private Rectangle[,] rectPixels;

        public SqImgLayerView()
        {
            InitializeComponent();
        }

        public SqImgLayerView(SqImgLayer layer) : this()
        {
            this.layer = layer;
            UpdateWorkingArea();
        }

        private void ScaleChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {

            //UpdateWorkingArea();

        }

        private void UpdateWorkingArea()
        {
            var LatticeHeight = layer.Height;
            var LatticeWidth = layer.Width;
            double scale = 2;
            LatticeCanvas.Width = LatticeWidth * scale;
            LatticeCanvas.Height = LatticeHeight * scale;
            rectPixels = new Rectangle[LatticeWidth, LatticeHeight];
            IsGridView = scale >= GridViewScaleValue;
            LatticeCanvas.Children.Clear();

            // Show color of reactor
            foreach (SqRt sqRt in layer)
            {
                Color color = sqRt.Input.Color;
                Rectangle newRect = new Rectangle() { Fill = new SolidColorBrush(color), StrokeThickness = 0, Height = scale, Width = scale, Tag = sqRt };
                newRect.MouseLeftButtonDown += newRect_MouseLeftButtonDown;
                Canvas.SetLeft(newRect, sqRt.X * scale);
                Canvas.SetTop(newRect, sqRt.Y * scale);
                LatticeCanvas.Children.Add(newRect);
                rectPixels[sqRt.X, sqRt.Y] = newRect;
            }

            if (IsGridView)
            {
                for (int horizontalCount = 0; horizontalCount <= LatticeWidth; horizontalCount++)
                {
                    double x = horizontalCount * scale;
                    LatticeCanvas.Children.Add(new Line() { X1 = x, Y1 = 0, X2 = x, Y2 = LatticeCanvas.Height, Stroke = Brushes.Black });
                }

                for (int verticalCount = 0; verticalCount <= LatticeHeight; verticalCount++)
                {
                    double y = verticalCount * scale;
                    LatticeCanvas.Children.Add(new Line() { X1 = 0, Y1 = y, X2 = LatticeCanvas.Width, Y2 = y, Stroke = Brushes.Black });
                }
            }
        }

        private void newRect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Rectangle rect = sender as Rectangle;
            if (rect == null) return;

            rect.Stroke = Brushes.BlueViolet;
            rect.StrokeThickness = 0.3;
            SqRt sqRt = rect.Tag as SqRt;
            // Fire select this reactor
        }

        public SqImgLayer Layer
        {
            get 
            {
                    return layer; 
            }
            set 
            { 
                    layer = value; 
                    UpdateWorkingArea(); 
            }
        }
    }
}
